/**
 * common javascript util
 */
/* format string value with arguments */
String.prototype.format = String.prototype.f = function() {
    var s = this,
        i = arguments.length;

    while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return s;
};

String.prototype.reverse = function() {
    return this.split('').reverse().join('');
};

/* if a string ends with a given suffix */
String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

/* if a string starts with a given prefix */
String.prototype.startsWith = function(str) {
    return this.indexOf(str) === 0;
};

function yymmdd(val) {
    var date = new Date(val);
    var yyyy = date.getFullYear();
    var mm = date.getMonth() + 1; //January is 0!
    var dd = date.getDate();
    var hh = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();

    if (mm < 10) {
        mm = '0' + mm;
    }
    if (dd < 10) {
        dd = '0' + dd;
    }
    if (hh < 10) {
        hh = '0' + hh;
    }
    if (min < 10) {
        min = '0' + min;
    }
    if (sec < 10) {
        sec = '0' + sec;
    }

    if (window.t) {
        return t('date_yyyy_mm_dd').format(yyyy, mm, dd);
    } else {
        return "{0}-{1}-{2}".f(yyyy, mm, dd);
    }
}

function eam_arrange_wo_items(data) {
    var result = {};
    var temp = _.sortBy(data, function(x) {
        // list by date desc order
        return x.eam_timestamp;
    });

    _.each(temp, function(el, index, lis) {
        // date divider value
        var ddv = yymmdd(el.eam_timestamp);
        if (!result[ddv]) result[ddv] = [];
        result[ddv].push(el);
    });
    return result;
}
